
//
var vm = new Vue({
    el: "#app",
    data: {
        goodsNum: 0, // 商品的价格
        item_list: [],//
        zaishou_num: '',//在收数量
        price:'0',//商品单价
    },
    computed:{
        freeze:function(){
            return this.price*this.goodsNum
        }
    },
    methods: {
        // 返回
        back:function(){
            // window.location.href = '/view/my/fabu_shougou.html'
            document.referrer
        },
        // 点击提交修改，触发submitBtn函数
        submitBtn: function () {
            // swal({
            //     title: "商品管理",
            //     text: "您已成功修改商品的数量！",
            //     timer: 800,
            //     buttons: false,
            // })
            // console.log(this.goodsNum);
            var that = this;
            vant.Dialog.confirm({
                message: '增加在收数量需要支付额外的冻结金,确认修改？',
            }).then(function () {
                var num = (+that.goodsNum);
                var value = {
                    num: num,
                    id: that.item_list[0].id
                }
                console.log(value);
                $.post(app.webUrl + '/view/api_card/edit_num', value, function (ret) {
                    if (ret.code == '1') {
                        vant.Toast('修改成功');
                        that.zaishou_num = that.goodsNum;
                        that.item_list[0].num = +that.item_list[0].buyed_num + (+that.goodsNum)
                        // 返回上一页
                        setTimeout(function(){
                            document.getElementById('back').click();
                        },1000)
                    } else {
                        vant.Toast('修改失败');
                    }
                });
            }).catch(function(){
                // return;
            });

        },

        // 修改商品的数量
        // value的值为1或2，为1时，点击的是减号，为2时，点击的是加号
        changeNum: function (value) {
            if (value == 1) {
                if (this.goodsNum != 0) {
                    this.goodsNum--;
                }
            } else {
                this.goodsNum++;
            }
        },
        init: function () {
            // 得到item//
            var id = app.GetQueryValue('id');
            this.get_item();
        },
        get_item:function(id){
            var item = JSON.parse(window.localStorage.getItem('goodsItem'));
            this.item_list = [];
            this.item_list.push(item);
            this.zaishou_num = item.num - item.buyed_num;
            this.goodsNum = this.zaishou_num;
            this.freeze = item.freeze;
            this.price = item.amount;
        }
    },
    mounted() {
        this.init();
    },

});